package com.lagou.util;

import java.sql.*;

public class DbUtil {
    private static String jdbcName;  //用于描述驱动信息
    private static String dbUrl;  //用于描述URL信息
    private static String dbUserName; //用于描述用户信息
    private static String dbPassword; //用户描述密码信息

    //进行静态成员的初始化操作
    static {
        jdbcName = "com.mysql.cj.jdbc.Driver";
        dbUrl = "jdbc:mysql://localhost:3306/db_web?characterEncoding=UTF-8";
        dbUserName="root";
        dbPassword = "123456";
        try {
            Class.forName(jdbcName);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    /**
     * 获取连接
     * @return
     * @throws SQLException
     */
    public static Connection getConnetion() throws SQLException {
        Connection con = DriverManager.getConnection(dbUrl, dbUserName, dbPassword);
        return con;
    }

    /**
     * 关闭连接
     * @param con
     * @throws SQLException
     */

    public static void closeResource(Connection con, PreparedStatement preparedStatement) {
        try {
            if(null != con){
                con.close();
            }
            if(null != preparedStatement){
                preparedStatement.close();
            }

        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    public static void closeResource(Connection con, PreparedStatement preparedStatement, ResultSet resultSet) {
        try {
            if(null != con){
                con.close();
            }
            if(null != preparedStatement){
                preparedStatement.close();
            }
            if(null != resultSet){
                resultSet.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
